//
//  PBNavigationBar.m
//  PayBox
//
//  Created by Hayder on 2022/2/10.
//

#import "HHNavigationBar.h"
#import "HHGlobalDefine.h"

@implementation HHNavigationBar

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height)];
        [self addSubview:self.backgroundImageView];
        
        UIImage *bgImage = [UIImage gradientColorImageFromColors:@[ColorHexString(@"#FF6731"),ColorHexString(@"#FF6731"),ColorHexString(@"#FF444B"),ColorHexString(@"#FF444B")] gradientType:GradientTypeTopToBottom imgSize:CGSizeMake(self.width, self.height)];
        self.backgroundImageView.image = bgImage;
        
        CGFloat titleW= 200;
        CGFloat margin = 15;
        self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake((HH_SCREEN_WIDTH - titleW)/2, [UIDevice vg_safeDistanceTop], titleW, self.navBarHeight-[UIDevice vg_safeDistanceTop])];
        self.titleLabel.textColor = kGray_51;
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        self.titleLabel.font = Font(18);
        [self addSubview:self.titleLabel];
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setImage:BundlePngImage(@"hh_nav_back") forState:UIControlStateNormal];
        button.frame = CGRectMake(margin, self.titleLabel.top, 50, self.titleLabel.height);
        button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        button.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
        [button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];
        self.backButton = button;
        
        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, self.height-1, HH_SCREEN_WIDTH, 1)];
        line.backgroundColor = kGray_230;
        [self addSubview:line];
        self.line = line;
    }
    
    return self;
}

/**
 添加标题
 */
- (void)addTitle:(NSString *)title
{
    self.titleLabel.text = title;
}

/**
 
 */
- (void)addRightBtnWithCustomer:(UIView *)view index:(NSInteger)index
{
    CGFloat margin = 15;
    view.frame = CGRectMake(self.width-(margin+view.width)*index, self.titleLabel.top, view.width, self.titleLabel.height);
    [self addSubview:view];
}

- (void)addRightBtnWithTitleOrImage:(NSArray <UIButton *>*)items
{
    if(items.count >2 || items.count == 0)
    {
        NSLog(@"最大支持2个");
        return;
    }
    UIButton *btn1;
    UIButton *btn2;
    if(items.count == 1)
    {
        btn1 = items[0];
        btn1.x = self.width - 10 - btn1.width;
        [self addSubview:btn1];
    }
    
    if(items.count == 2)
    {
        btn1 = items[0];
        btn2 = items[1];
        btn1.x = self.width - 10 - btn1.width;
        btn2.x = btn1.left - btn2.width;
        [self addSubview:btn1];
        [self addSubview:btn2];
    }
}

- (UIButton *)createBtnWithTitle:(NSString *)title color:(UIColor *)color target:(id)target action:(SEL)action
{
    CGFloat fontSize = 14;
    CGFloat width = [title sizeWithFont:Font(fontSize) maxW:MAXFLOAT].width+5;
    UIButton *btn = [UIButton buttonWithUIProperty:HHButtonProperty.init().frame(CGRectMake(0, self.titleLabel.top, width, self.titleLabel.height))
    .title(title)
    .fontSize(fontSize)
    .titleColor(color)
    .targetAction(target,action)];
    return btn;
}
- (UIButton *)createBtnWithImage:(UIImage *)image target:(id)target action:(SEL)action
{
    UIButton *btn = [UIButton buttonWithUIProperty:HHButtonProperty.init().frame(CGRectMake(0, self.titleLabel.top, 40, self.titleLabel.height))
    .targetAction(target,action)];
    [btn setImage:image forState:UIControlStateNormal];
    return btn;
}

- (void)back
{
    [self.viewController.navigationController popViewControllerAnimated:YES];
}

- (void)setNavigationBarColor:(UIColor *)color
{
    UIImage *bgImage = [UIImage imageWithColor:color];
    self.backgroundImageView.image = bgImage;
}

- (void)backBtnCancelEvent
{
    [self.backButton removeTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
}

- (void)setBackButtonColor:(UIColor *)color
{
    UIImage *backImage = [BundlePngImage(@"hh_nav_back") pb_imageWithTintColor:color];
    [self.backButton setImage:backImage forState:UIControlStateNormal];
}

- (void)setNavigationBarClearAndWhiteTitleWithFont:(UIFont *__nullable) font
{
    UIImage *bgImage = [UIImage imageWithColor:UIColor.clearColor];
    self.backgroundImageView.image = bgImage;
    self.titleLabel.textColor = UIColor.whiteColor;
    if(font)
    {
        self.titleLabel.font = font;
    }
    [self.backButton setImage:[BundlePngImage(@"hh_nav_back") pb_imageWithTintColor:UIColor.whiteColor] forState:UIControlStateNormal];
}

@end
